const mysql = require('mysql');
const db = require('./mysqlConfig');

exports.submitUserReview = (reviewData, callback) => {
    const { user_id, room_id, rating, text_review, images } = reviewData;

    const sql = `
        INSERT INTO user_reviews (user_id, room_id, rating, text_review, images, created_time)
        VALUES (?, ?, ?, ?, ?, NOW())
    `;

    const imagesJson = images ? JSON.stringify(images) : null;

    db.query(sql, [user_id, room_id, rating, text_review, imagesJson], (error, result) => {
        if (error) return callback(error, null);

        const newReview = {
            review_id: result.insertId,
            ...reviewData,
            created_time: new Date().toISOString()
        };

        callback(null, newReview);
    });
};
